package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



import com.hmall.common.enums.AppHttpCodeEnum;
import com.hmall.common.result.PageResult;
import com.hmall.common.result.ResponseResult;

import com.hmall.item.mapper.ItemMapper;


import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult delItem(Long id) {
        //非空判断
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据id删除
        this.removeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 修改商品
     * @param item
     * @return
     */
    @Override
    public ResponseResult modById(Item item) {
        //非空判断
        if (item == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //修改商品
        this.updateById(item);
        return ResponseResult.okResult();
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @Override
    public ResponseResult<Item> itemById(Long id) {
        //非空判断
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据id查询
        Item item = this.getById(id);
        return ResponseResult.okResult(item);
    }

    /**
     * 商品上下架
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResponseResult upOrDownItem(Long id, Integer status) {
        //非空判断
        if (id == null) {
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //更新状态
        Item item = Item.builder()
                .id(id)
                .status(status)
                .build();

        this.updateById(item);
        return ResponseResult.okResult();
    }

    /**
     * 新增商品
     * @param item
     */
    @Override
    public ResponseResult addItem(Item item) {
        if (item == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        item.setUpdateTime(new Date());
        item.setCreateTime(new Date());
        this.save(item);
        return ResponseResult.okResult();
    }

    /**
     * 商品分页
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult pageItem(Integer page,Integer size) {
        //为空，则设置默认
        if (page == null) {
            page=1;
        }
        if (size == null) {
            size=5;
        }
        //创建查询
        LambdaQueryWrapper<Item> query = new LambdaQueryWrapper<>();

        //排序
        query.orderByDesc(Item::getUpdateTime);

        //分页查询商品
        IPage<Item> pages = new Page<>(page, size);

        IPage<Item> iPage = this.page(pages,query);

        //封装
        return new PageResult(iPage.getTotal(),iPage.getRecords());
    }
}
